﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Helpers;
using Model;
using System.Collections.Generic;

namespace FEIStorageSite.Admin
{
    public partial class Messages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                DAL.connectionString = "Data Source=" + Server.MapPath("App_Data\\fei_storage.db");

                switch (Request["action"])
                {
                    case "edit":
                        if (!IsPostBack)
                        {
                            int messageID = Convert.ToInt32(Request["id"]);
                            MessageItem message = DAL.GetMessage(messageID);

                            idLabel.Text = message.ID.ToString();
                            ipLabel.Text = message.Ip;
                            dateLabel.Text = message.Date.ToString();
                            authorNameTextBox.Text = message.AuthorName;
                            messageTextBox.Text = message.MessageText;
                            editMessageTable.Visible = true;
                        }
                        break;
                    case "delete":
                        int messageDeleteID = Convert.ToInt32(Request["id"]);
                        DAL.DeleteMessage(messageDeleteID);
                        Response.Redirect("Messages.aspx?action=confirm_delete");
                        break;
                    case "delete_all":
                        DAL.DeleteAllMessages();
                        Response.Redirect("Messages.aspx");
                        break;
                    case "confirm_delete":
                        messagesPageError.Text = "Повідомлення успішно видалено";
                        break;
                }
            }
            catch (Exception exception)
            {
                messagesPageError.Text = exception.Message;
            }
        }

        protected void UpdateButtonClick(object sender, EventArgs e)
        {
            try
            {
                MessageItem message = new MessageItem();
                message.ID = Convert.ToInt32(idLabel.Text);
                message.AuthorName = authorNameTextBox.Text;
                message.MessageText = messageTextBox.Text;
                message.Ip = ipLabel.Text;
                message.Date = Convert.ToDateTime(dateLabel.Text);
                DAL.EditMessage(message);
                throw new Exception("Зміни успішно збережено");
            }
            catch (Exception exception)
            {
                messagesPageError.Text = exception.Message;
            }
        }

        public void MessagesDataPagerPreRender(object sender, EventArgs e)
        {
            if (Request["action"] == null || Request["action"] == "confirm_delete")
            {
                List<MessageItem> messages = DAL.GetMessagesList();

                if (messages.Count != 0)
                {
                    messagesListView.DataSource = messages;
                    messagesListView.DataBind();
                    deleteAllButton.Visible = true;
                }
                else
                {
                    messagesPageError.Text = "Поки що повідомлень немає";
                }
            }
        }
    }
}
